package com.party.notify.appNotify.service.impl;

import com.alibaba.fastjson.JSON;
import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.core.model.message.IdentityType;
import com.party.core.model.message.MessageType;
import com.party.core.service.message.IMessageService;
import com.party.notify.appNotify.model.AppMessage;
import com.party.notify.appNotify.service.IAppNotifyService;
import com.party.notify.jms.publisher.service.INotifyPublisherService;
import com.party.notify.template.service.ITemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 *
 * app消息推送服务接口
 * User: wei.li
 * Date: 2017/4/5
 * Time: 23:21
 */

@Service
public class AppNotifyService implements IAppNotifyService {

    @Autowired
    private ITemplateService templateService;

    @Autowired
    private INotifyPublisherService notifyPublisherService;

    @Autowired
    private IMessageService messageService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 消息发送
     * @param template
     * @param content
     */
    public void push(String template,String authorId, String type, HashMap<String, Object> content) {
        String sysMessageId = content.get("sysMessageId") != null ? content.get("sysMessageId").toString() : "";
        String message = templateService.replace(template, content);
        if (StringUtils.isNotEmpty(sysMessageId)) {
            message = content.get("{title}").toString();
            type = content.get("messageType").toString();
        }
        AppMessage appMessage = new AppMessage();
        Object tempAppMessageId = content.get("tempAppMessageId");
        if (null != tempAppMessageId) {
            appMessage.setId((String) tempAppMessageId);
        }
        appMessage.setMessageType(type);
        appMessage.setMemberId((String) content.get("createBy"));
        appMessage.setTitle(message);
        appMessage.setChannelType(Constant.MESSAGE_CHANNEL_APP);
        appMessage.setLogo((String) content.get("logo"));
        appMessage.setOrderId((String) content.get("orderId"));
        appMessage.setOrderStatus((Integer) content.get("orderStatus"));
        appMessage.setRelId((String) content.get("relId"));
        appMessage.setTag((String) content.get("tag"));
        appMessage.setPicUrl((String) content.get("url"));
        appMessage.setContent((String) content.get("content"));
        appMessage.setCreateBy(authorId);
        appMessage.setUpdateBy(authorId);

        String identityType = (String) content.get("identityType");
        identityType = identityType == null ? IdentityType.NORMAL.getCode() : identityType;
        appMessage.setIdentityType(identityType);

        if (StringUtils.isNotEmpty(type) && MessageType.SYS.getCode().equals(type)) {
            logger.info("插入message消息：{}", JSON.toJSONString(appMessage));
            messageService.insert(AppMessage.transform(appMessage));
        } else {
            notifyPublisherService.send(appMessage, Constant.MESSAGE_CHANNEL_APP, true);
        }
    }


}
